package com.spider.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HttpUtils {
    private final String url;
    private final Map<String, String> params;

    private Map<String, String> headers;

    public HttpUtils(String url, Map<String, String> params) {
        this.url = url;
        this.params = params;
    }

    public HttpUtils(String url, Map<String, String> headers, Map<String, String> params) {
        this.url = url;
        this.headers = headers;
        this.params = params;
    }

    /**
     * 向接口请求数据/获取网页源码
     *
     * @param isEncode 是否已经过URL编码
     * @return 返回响应数据
     */
    public String getData(boolean isEncode) {
        try {
            return getInstance(isEncode).body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 获取实例对象
     *
     * @param isEncode 是否已经过URL编码
     * @return 返回Response对象
     */
    private Response getInstance(boolean isEncode) {
        Request request;
        OkHttpClient okHttpClient = new OkHttpClient();

        Request.Builder requestBuilder = new Request.Builder();
        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();

        //添加参数
        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (isEncode) {
                    urlBuilder.addEncodedQueryParameter(entry.getKey(), entry.getValue());
                } else {
                    urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
                }
            }
        }

        try {

            if (headers != null) {
                requestBuilder.headers(Headers.of(headers));
            }

            request = requestBuilder.url(urlBuilder.build()).get().build();

            Call call = okHttpClient.newCall(request);
            Response response = call.execute();

            //判断是否响应成功
            if (response.code() == HttpURLConnection.HTTP_OK) {
                return response;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 获取接口响应体
     *
     * @param path   接口地址
     * @param params 参数
     * @param isEncode 是否已经过URL编码
     * @return 将响应体已JSONObject对象的形式返回
     */
    public static JSONObject getResponse(String path, Map<String, String> params, boolean isEncode) {
        String response = new HttpUtils(path, params).getData(isEncode);

        return JSON.parseObject(response);
    }

    /**
     * 获取接口响应体
     *
     * @param path     接口地址
     * @param params   参数
     * @param isEncode 是否已经过URL编码
     * @return 将响应体已JSONObject对象的形式返回
     */
    public static JSONObject getResponse(String path, Map<String, String> headers, Map<String, String> params, boolean isEncode) {
        String response = new HttpUtils(path, headers, params).getData(isEncode);

        return JSON.parseObject(response);
    }
}
